<template>
    <div class="formLibrary">
        <com 
            :is="comChoose(formItem.fieldType, formItem)" 
            @handleChange='handleChange' 
            :iptAttritube='formItem' 
            ref='coms'
            @uploadProgres='uploadProgres'
            @calculationChildren="calculationChildren"
        ></com>
    </div>
</template>

<script>
import ipt from './input.vue'
import inputNumber from './inputNumber.vue'
import datePicker from './datePicker.vue'
import upload from './upload.vue'
import selectRadio from './selectRadio.vue'
import selectChecked from './selectChecked.vue'
import selectSearch from './selectSearch.vue'
import meritIpt from './meritIpt.vue'
import formula from './formulaCalculation.vue'
export default {
    props: {
        formItem: {
            type: Object,
            default: () => {}
        },
        defaultData: {
            type: Array,
            default: () => {}
        },
        rowId: {
            type: Number,
            default: 0
        }
    },
    data() {
        return {
            
        }
    },
    components: {
        ipt,
        inputNumber,
        datePicker,
        upload,
        selectRadio,
        selectChecked,
        meritIpt,
        selectSearch,
        formula
    },
    mounted() {
        this.$nextTick(() => {
            if(!this.formItem.cusField) {
                this.$refs.coms.defaultData(this.defaultData)
            }
        })
    },
    methods: {
        handleChange(d, relevanceObject) {
            let n = this.formItem.cusField ? this.formItem.cusField : this.formItem.id
            let obj = {
                name: relevanceObject == 0 ? this.formItem.name : n,
                value: d.value,
                relevanceObject: relevanceObject,
                state: d.state,
            }
            this.$emit('formItemHandle', obj,this.rowId)
        },
        comChoose(c, d) {
            if(!d.cusField) {
                if(c == 'picture') {
                    return 'upload'
                }
                if(c == 'relevanceObject') {
                    return 'selectRadio'
                }
                if(c == 'relevanceProp') {
                    return 'ipt'
                }
                return c
            } else {
                if(d.customFieldMess != null) {
                    return d.fieldType
                } else if(d.list != null) {
                    return 'selectSearch'
                } else {
                    return 'meritIpt'
                }
            }
        },
        uploadProgres(s) {
            this.$emit('uploadProgress', s)
        },
        removeData(type) {
            this.$refs.coms.removeSure(type)   
        },
        calculationChildren(name,id) {
            this.$emit('getFormula',name,id)
        }
    },
    watch: {
        defaultData: {
            handler(val) {
                this.$refs.coms.defaultData(val)
            },
            deep: true
        },
    }
}
</script>